package cn.fw.community.adapter.facade.track.web;

import cn.fw.community.adapter.vo.TestVo;
import cn.fw.community.client.model.enums.SexEnum;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.stream.IntStream;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2022/6/27 13:50
 */
@RestController
@Slf4j
@Validated
@RequiredArgsConstructor
@RequestMapping("/track/test")
public class TrackTestController {

  private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
  @Autowired
  @Qualifier("ttlTaskExecutor")
  private Executor ttlThreadPoolTaskExecutor;

  @GetMapping("/{id}")
  public String detail(@PathVariable("id") String id) {
    return "this is " + id;
  }

  @PutMapping("/{sex}")
  public SexEnum update(@PathVariable("sex") SexEnum sex) {
    return sex;
  }

  @GetMapping("/time/{t}")
  public LocalDateTime time(@PathVariable("t") LocalDateTime t) {
    return t;
  }

  @GetMapping("/min")
  public String testMin(@RequestParam("m") @Min(message = "不能比较傲232", value = 3) Long min) {
    return "我输入了:" + min;
  }

  @GetMapping("/header")
  public String testHeader(@RequestHeader("m") String notnull) {
    return "我输入了:" + notnull;
  }

  @PostMapping
  @SneakyThrows
  public TestVo save(@RequestBody TestVo test) {

    List<TestVo> testVos = new ArrayList<>();
    ObjectMapper objectMapper = new ObjectMapper();

    String s = objectMapper.writeValueAsString(testVos);
    List<TestVo> testVos1 = objectMapper.readValue(s, new TypeReference<List<TestVo>>() {
    });

    if (test.getSex() != null) {
      test.setSexString("只是" + test.getSex().getDesc());
    }

    threadPoolTaskExecutor.execute(() -> {
      log.info("{}:这是异步操作", Thread.currentThread().getName());
    });
    ttlThreadPoolTaskExecutor.execute(() -> {
      log.info("{}:这是ttl异步操作", Thread.currentThread().getName());
    });
    return test;
  }


  public static void main(String[] args) throws InterruptedException {
//    List<BaseTree> tempList = new ArrayList<>();
//    BaseTree baseTree1 = new BaseTree();
//    baseTree1.setCode("1");
//    baseTree1.setName("a");
//    baseTree1.setParentCode("");
//    tempList.add(baseTree1);
//
//    BaseTree baseTree2 = new BaseTree();
//    baseTree2.setCode("2");
//    baseTree2.setName("b");
//    baseTree2.setParentCode("");
//    tempList.add(baseTree2);
//
//    BaseTree baseTree3 = new BaseTree();
//    baseTree3.setCode("3");
//    baseTree3.setName("c");
//    baseTree3.setParentCode("1");
//    tempList.add(baseTree3);
//
//    BaseTree baseTree4 = new BaseTree();
//    baseTree4.setCode("4");
//    baseTree4.setName("d");
//    baseTree4.setParentCode("2");
//    tempList.add(baseTree4);
//
//    BaseTree baseTree5 = new BaseTree();
//    baseTree5.setCode("5");
//    baseTree5.setName("e");
//    baseTree5.setParentCode("3");
//    tempList.add(baseTree5);
//
//    BaseTree baseTree6 = new BaseTree();
//    baseTree6.setCode("6");
//    baseTree6.setName("er");
//    baseTree6.setParentCode("3");
//    tempList.add(baseTree6);
//    System.out.println(JSONUtil.toJsonStr(buildTree(tempList, "")));

    IntStream.range(0, 10000).parallel().map(e -> {
      int i = e + 10000;
      if (i > 11000) {
        log.info("我大于了15000:{}", i);
      }
      return i;
    }).anyMatch(e -> e > 11000);

    log.info("我结束了，我全部都执行完了？");
    Thread.sleep(10000);
  }
}
